﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace EasySL.Controls
{
    public partial class ExDataGrid : DataGrid
    {
        private void BindExpandEvent(ExCommandPanel cp)
        {
            var expandColumnMappings = new Dictionary<string, List<ExDataGridColumn>>();
            
            foreach (ExDataGridColumn column in this.Columns)
            {
                if (column != null && !string.IsNullOrEmpty(column.Parent))
                {
                    var parentColumn = this.Columns.OfType<ExDataGridColumn>().SingleOrDefault(c => string.Equals(c.BindProperty, column.Parent, StringComparison.OrdinalIgnoreCase));
                    column.Visibility = Visibility.Collapsed;
                    if (!expandColumnMappings.Keys.Contains(parentColumn.BindProperty))
                    {
                        parentColumn.EnableExpand = true;
                        expandColumnMappings.Add(parentColumn.BindProperty, new List<ExDataGridColumn>());
                    }
                    expandColumnMappings[parentColumn.BindProperty].Add(column);
                }
            }
            
            cp.OnExpand += (bindProperty, isExpand) =>
            {
                foreach (ExDataGridColumn column in expandColumnMappings[bindProperty])
                    column.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;

                this.UpdateLayout();
            };
        }
    }
}
